package mini;
/* this is the cup file for the mini language
 * at http://www.cs.drexel.edu/~jjohnson/2010-11/spring/cs550/lectures/lec2c.html *
 * created by Xu, 2/5/07
 * 
 * Modified by Mike Kopack for CS550, 2009 Spring Qtr.
 * Should be at the same level of completeness as the Lecture 2c
 * C++ version.
 */


import java.util.*;

import mini.expr.Element;
import mini.oo.MiniClassDefinition;

public class Program {

    private StatementList stmtlist;
    
    public Program(StatementList list) {
        stmtlist = list;
    }

    public void eval(List<Env> listOfEnvs, LinkedList var) {
        try {
            stmtlist.eval(listOfEnvs, var);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public void dump(List<Env> listOfEnvs, LinkedList var) {
        //System.out.println(hm.values());
        System.out.println("Dumping out all the variables...");
        
        // Grab nametable an functiontable off the frame
        HashMap<String,Element> nametable = null;
        HashMap<String,Proc> functiontable = null;
        HashMap<String,MiniClassDefinition> classtable = null;
        if( listOfEnvs != null && listOfEnvs.size() > 0 ) {
        	nametable = listOfEnvs.get(0).getNametable();
        	functiontable = listOfEnvs.get(0).getFunctable();
        	classtable = listOfEnvs.get(0).getClasstable();
        }
        
        if (nametable != null) {
            for (String name : nametable.keySet()) {
                if (name.endsWith("expected"))
                    continue;
                
                String expectedName = name + "expected";
                Element expectedValue = nametable.get(expectedName);
                Element value = nametable.get(name);
                StringBuilder outputBuilder = new StringBuilder(name + "=" + value);
                
                if (expectedValue != null) {
                    outputBuilder.append((value.toString().equals(expectedValue.toString()) ? "" : " <<=== Error. Value does not equal expected. Expected = " + expectedValue.toString()));
                }
                
                System.out.println(outputBuilder.toString());
            }
        }
        if (functiontable != null) {
            for (String name : functiontable.keySet()) {
                System.out.println("Function: " + name + " defined...");
            }
        }
        if (classtable != null) {
            for (String name : classtable.keySet()) {
                System.out.println("Class: " + name + " defined...");
            }
        }
    }
}
